package Client;

import utils.AccountAddFriendInfoList;
import utils.FriendArrayList;

import java.io.*;
import java.net.Socket;


/**该类可获得用户好友列表，只需要传入用户id初始化该类，执行main中try的函数，即可从服务端获得好友列表FriendArrayList*/
public class GetFriendArrayListActionListener {
    private FriendArrayList friendArrayList;
    private String userId;

    public GetFriendArrayListActionListener(String userId){
        this.userId = userId;
        try {
            Socket socket = new Socket(ServerInfo.SERVER_IP_ADDRESS, ServerInfo.SERVER_GETFRIENDARRAYLIST_PORT);
            OutputStream outputStream = socket.getOutputStream();//获取一个输出流，向服务端发送信息
            PrintWriter printWriter=new PrintWriter(outputStream);//将输出流包装成打印流
            printWriter.println(userId);//此处将当前用户的id发送给服务端
            printWriter.flush();
            //socket.shutdownOutput();//关闭输出流

            InputStream inputStream = socket.getInputStream();
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            //下面这一行接收服务端返回的发送给当前用户的消息队列
            this.setFriendArrayList((FriendArrayList) objectInputStream.readObject());
            outputStream.close();
            printWriter.close();
            inputStream.close();
            objectInputStream.close();
            socket.close();

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public FriendArrayList getFriendArrayList() {
        return friendArrayList;
    }

    public void setFriendArrayList(FriendArrayList friendArrayList) {
        this.friendArrayList = friendArrayList;
    }

}
